package com.canbot.u05.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;

/**
 * 通用提示框
 */

public class CommonDialog {

        private Context context;

        public CommonDialog(Context context) {
                this.context = context;
        }

        /**
         * 提示对话框 两个button
         *
         * @param hintText
         * @param onSelect
         * @return
         */
        public AlertDialog hintDialog(String hintText, String hintText1, final OnSelectListener onSelect) {
                //创建对话框对象
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                //初始化空降
                View view = LayoutInflater.from(context).inflate(R.layout.dialog_warn2, null);
                RelativeLayout llhintDialog = (RelativeLayout) view.findViewById(R.id.ll_warnDialog);
                TextView tvhintText = (TextView) view.findViewById(R.id.text_hint);
                TextView tvhintText1 = (TextView) view.findViewById(R.id.text_hint1);
                Button btcancle = (Button) view.findViewById(R.id.dialog_cancel);
                Button btensure = (Button) view.findViewById(R.id.dialog_sure);
                //初始化数据
                tvhintText.setText(hintText);
                tvhintText1.setText(hintText1);
                //定义监听事件
                builder.setView(view);
                final AlertDialog dialog = builder.create();
                btcancle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                onSelect.onCancle(v);
                                dialog.dismiss();
                        }
                });
                btensure.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                onSelect.onEnsure(v);
                                dialog.dismiss();
                        }
                });
                //显示dialog
                dialog.show();
                //获取屏幕宽高
                WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                Display defaultDisplay = wm.getDefaultDisplay();
                int screenWidth = defaultDisplay.getWidth();
                int screenHeight = defaultDisplay.getHeight();
                //重新设置布局高度，可以理解为设置dialog高度
                ViewGroup.LayoutParams params = llhintDialog.getLayoutParams();
                params.height = (int) (screenHeight * 0.55);
                llhintDialog.setLayoutParams(params);
                //设置dialog宽度
                Window window = dialog.getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                window.setLayout((int) (screenWidth * 0.5), attributes.height);
                //设置dialog背景为透明色
                window.setBackgroundDrawableResource(R.color.transparent);

                return dialog;
        }

        /**
         * 带输入框的提示框框
         *
         * @param onSelect
         * @return
         */
        public AlertDialog inputDialog(final OnSelectListener onSelect) {
                //创建对话框对象
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                //初始化空降
                View view = LayoutInflater.from(context).inflate(R.layout.dialog_input2, null);
                RelativeLayout llinputDialog = (RelativeLayout) view.findViewById(R.id.ll_warnDialog);
                Button tvensure = (Button) view.findViewById(R.id.dialog_sure);
                Button tvcancle = (Button) view.findViewById(R.id.dialog_cancel);
                final EditText etinput = (EditText) view.findViewById(R.id.et_input);

                //定义监听事件
                builder.setView(view);
                final AlertDialog dialog = builder.create();
                tvcancle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                onSelect.onCancle(v);
                                dialog.dismiss();
                        }
                });
                tvensure.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                onSelect.onEnsure(v);
                                dialog.dismiss();
                        }
                });

                etinput.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {

                        }
                });
                //显示dialog
                dialog.show();
                //获取屏幕宽高
                WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                Display defaultDisplay = wm.getDefaultDisplay();
                int screenWidth = defaultDisplay.getWidth();
                int screenHeight = defaultDisplay.getHeight();
                //重新设置布局高度，可以理解为设置dialog高度
                ViewGroup.LayoutParams params = llinputDialog.getLayoutParams();
                params.height = (int) (screenHeight * 0.55);
                llinputDialog.setLayoutParams(params);
                //设置dialog宽度
                Window window = dialog.getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                window.setLayout((int) (screenWidth * 0.5), attributes.height);
                //设置dialog背景为透明色
                window.setBackgroundDrawableResource(R.color.transparent);

                return dialog;
        }

        /**
         * 只有一个按钮的提示框
         *
         * @param hintText
         * @param ensure
         * @return
         */
        public AlertDialog warnDialog(String hintText, final OnEnsureListener ensure) {
                //创建对话框对象
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                //初始化空降
                View view = LayoutInflater.from(context).inflate(R.layout.dialog_warn, null);
                TextView tvensure = (TextView) view.findViewById(R.id.tv_ensure);
                TextView tvhintText = (TextView) view.findViewById(R.id.tv_hintText);
                //初始化数据
                tvhintText.setText(hintText);
                //定义监听事件
                builder.setView(view);
                final AlertDialog dialog = builder.create();
                tvensure.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                if (ensure != null) {
                                        ensure.onEnsure(v);
                                }
                                dialog.dismiss();
                        }
                });
                //显示dialog
                dialog.show();
                return dialog;
        }

        /**
         * 只有一个按钮的提示框
         *
         * @param ensure
         * @return
         */
        public AlertDialog warnDialog(final OnEnsureListener ensure) {
                //创建对话框对象
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                //初始化空降
                View view = LayoutInflater.from(context).inflate(R.layout.dialog_vip_connecting, null);
                Button sure = (Button) view.findViewById(R.id.goto_face);
                //定义监听事件
                builder.setView(view);
                final AlertDialog dialog = builder.create();
                sure.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                if (ensure != null) {
                                        ensure.onEnsure(v);
                                }
                                dialog.dismiss();
                        }
                });
                //显示dialog
                dialog.show();
                //获取屏幕宽高
                WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                Display defaultDisplay = wm.getDefaultDisplay();
                int screenWidth = defaultDisplay.getWidth();
                //设置dialog宽度
                Window window = dialog.getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                window.setLayout((int) (screenWidth * 0.5), attributes.height);
                //设置dialog背景为透明色
                window.setBackgroundDrawableResource(R.color.transparent);


                return dialog;
        }

        /**
         * 无按钮的提示框
         *
         * @return
         */
        public Dialog noDialog(String title) {

                LayoutInflater inflater = LayoutInflater.from(context);
                View v = inflater.inflate(R.layout.dialog_tip1, null);// 得到加载view
                RelativeLayout layout = (RelativeLayout) v
                        .findViewById(R.id.ll_tipDialog);// 加载布局
                TextView tipTextView = (TextView) v.findViewById(R.id.tv_hintText);// 提示文字
                tipTextView.setText(title);// 设置加载信息

                Dialog loadingDialog = new Dialog(context, R.style.MyDialogStyle);// 创建自定义样式dialog
                loadingDialog.setCancelable(true); // 是否可以按“返回键”消失
                loadingDialog.setCanceledOnTouchOutside(false); // 点击加载框以外的区域
                loadingDialog.setContentView(layout);// 设置布局

                loadingDialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
                /**
                 *将显示Dialog的方法封装在这里面
                 */

                loadingDialog.show();

                return loadingDialog;
        }

        public interface OnSelectListener {

                void onCancle(View view);

                void onEnsure(View view);
        }

        public interface OnEnsureListener {

                void onEnsure(View view);
        }

}
